package org.misterhou.spring.boot.demo.websocket;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/server/{userId}")
@Slf4j
@Component
public class WebSocketServer {

    private static WebSocketClient webSocketClient;

    public static void setWebSocketClient(WebSocketClient client) {
        webSocketClient = client;
    }

    @OnOpen
    public void onOpen(Session session) {
        log.info("已连接");
    }

    @OnMessage
    public void onMessage(Session session, String message) {
        webSocketClient.sendMessage(message);
        webSocketClient.setProxySession(session);
        log.info("接收到的消息：{}", message);
    }

    @OnClose
    public void onClose(Session session) {
        log.info("已断开");
    }
}
